package ru.niir.goodfil.ui;

import ru.niir.goodfil.db.Brand;
import ru.niir.goodfil.db.ClientGroup;
import ru.niir.goodfil.utils.validation.ValidationError;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class ClientGroupsBean2 extends PageBean
{
    private List<Brand> brands = gf().getBrands();

    private Map<Long, List<ClientGroup>> clientGroups = new HashMap<Long, List<ClientGroup>>();

    private void myinit()
    {
        for (Brand brand : brands)
        {
            clientGroups.put(brand.getId(), gf().getClientGroupsByBrandId(brand.getId()));
        }
    }

    public ClientGroupsBean2()
    {
        myinit();
    }

    public void removeClientGroup()
    {
        long clientGroupId = Long("clientGroupId");
        try
        {
            gf().removeClientGroup(clientGroupId);
        }
        catch (ru.niir.goodfil.exceptions.RemoveFailureException e)
        {
            facesError("С клиентской группой связаны клиенты");
        }

        myinit();
    }

    public void createClientGroup()
    {
        long brandId = Long("brandId");
        String name = String("name");
        Float money = Float("money");

        try
        {
            ClientGroup clientGroup = new ClientGroup();
            clientGroup.setName(name);
            clientGroup.setCriteriaSum(money);
            clientGroup.setBrandId(brandId);
            gf().saveClientGroup(clientGroup);
        }
        catch (ru.niir.goodfil.exceptions.ValidationException e)
        {
            for (ValidationError error : e.getResult().getErrors()) facesError(error.toHumanString());
        }
        catch (ru.niir.goodfil.exceptions.CreateFailureException e)
        {
            facesError(e.getResult().getMsg());
        }

        myinit();
    }

    public void editClientGroup()
    {
        long clientGroupId = Long("clientGroupId");
        String name = String("name");
        Float money = Float("money");

        try
        {
            ClientGroup clientgroup = gf().getClientGroupById(clientGroupId);
            clientgroup.setName(name);
            clientgroup.setCriteriaSum(money);
            gf().updateClientGroup(clientgroup);
        }
        catch (ru.niir.goodfil.exceptions.ValidationException e)
        {
            for (ValidationError error : e.getResult().getErrors()) facesError(error.toHumanString());
        }

        myinit();
    }

    public void activateClientGroup()
    {
        long clientGroupId = Long("clientGroupId");

        try
        {
            ClientGroup clientgroup = gf().getClientGroupById(clientGroupId);
//      clientgroup.setactive = !clientgroup.active;
            gf().updateClientGroup(clientgroup);
        }
        catch (ru.niir.goodfil.exceptions.ValidationException e)
        {
            for (ValidationError error : e.getResult().getErrors()) facesError(error.toHumanString());
        }

        myinit();
    }

    public void showClientGroup()
    {
        long clientGroupId = Long("clientGroupId");

        try
        {
            ClientGroup clientgroup = gf().getClientGroupById(clientGroupId);
            clientgroup.setShown(!clientgroup.isShown());
            gf().updateClientGroup(clientgroup);
        }
        catch (ru.niir.goodfil.exceptions.ValidationException e)
        {
            for (ValidationError error : e.getResult().getErrors()) facesError(error.toHumanString());
        }

        myinit();
    }

    public List<Brand> getBrands()
    {
        return brands;
    }

    public void setBrands(List<Brand> brands)
    {
        this.brands = brands;
    }

    public Map<Long, List<ClientGroup>> getClientGroups()
    {
        return clientGroups;
    }

    public void setClientGroups(Map<Long, List<ClientGroup>> clientGroups)
    {
        this.clientGroups = clientGroups;
    }
}